<?php 
namespace Material\Controller;
use Admin\Controller\AdminbaseController;
/**
 * 应用分类管理
 * @author Haiqin
 *
 */
class CatsadminController extends AdminbaseController{
	
	protected $m;
	protected $mm;
	
	function _initialize() {
		parent::_initialize();
		$this->m = D('Cats');
		$this->mm = D('CatsGroup');
	}
	
	public function index(){
		if($_GET['action']){
			$_SESSION['cur_appcat_action'] = $_GET['action'];
		}
		if(!$_SESSION['cur_appcat_action']){
			$_SESSION['cur_appcat_action'] = 'add';
		}
		
		$map['uid'] = $_SESSION['loginAdmin']['uid'];

		$this->groups = $this->mm->where($mapp)->select();
		if($_GET['group_id']){
			$_SESSION['cur_select_group'] = $_GET['group_id'];
		}
		if(!$_SESSION['cur_select_group']){
			$_SESSION['cur_select_group'] = $this->groups[0]['group_id'];
		}
		$this->display();
	}
	
	public function indexgroup(){
		$this->display();
	}
	
	public function editgroup(){
		if($_GET['id']){
			$this->info = $this->mm->find($_GET['id']);
		}
		$this->display();
	}
	
	public function editgroup_post(){
		if(IS_POST){
			$_POST['update_time'] = date('Y-m-d H:i:s');
			if($_POST['group_id']){
				if($this->mm->save($_POST)){
					$this->_update_cache_groups();
					$this->success('更新成功');
				}else{
					$this->error('更新失败');
				}
			}else{
				if($this->mm->add($_POST)){
					$this->_update_cache_groups();
					$this->success('添加成功');
				}else{
					$this->error('添加失败');
				}
			}
		}
	}
	
	public function edit(){
		$tree = new \Think\Tree();
		$parentid = intval(I("get.parentid"));
		
		if($_GET['id']){
			$this->info = $this->m->find($_GET['id']);
			$parentid = $this->info['parentid'];
		}

		$map['group_id'] = $_SESSION['cur_select_group'];
		$data = $this->m->where($map)->order('listorder desc')->select();

		foreach ($data as $r) {
			$r['selected'] = $r['id'] == $parentid ? 'selected' : '';
			$array[] = $r;
		}
		$str = "<option value='\$id' \$selected>\$spacer \$cat_name</option>";
		$tree->init($array);
		$select_categorys = $tree->get_tree(0, $str);
		$this->assign("select_categorys", $select_categorys);
		
		$this->display();
	}
	
	public function edit_post(){
		if(IS_POST){
			$_POST['update_time'] = date('Y-m-d H:i:s');
			if($_POST['id']){
				if($this->m->save($_POST)){
					$this->_update_cache_tree($_POST['group_id']);
					$this->success('更新成功',U('Catsadmin/edit','id='.$_POST['id']));
				}else{
					$this->error('更新失败');
				}
			}else{
				if($this->m->add($_POST)){
					$this->_update_cache_tree($_POST['group_id']);
					$this->success('添加成功',U('Catsadmin/edit'));
				}else{
					$this->error('添加失败');
				}
			}
		}
	}
	
	
	/**
	 * 删除分类
	 */
	public function del(){
		$id = array_unique((array)I('ids',0));
		if ( empty($id) ) {
			$this->error('请选择要操作的数据!');
		}
		$map = array($this->m->getPk() => array('in', $id));
		$tmp = $this->m->where($map)->find(); //去除一个样本，获取到group_id和app_id;
		if($this->m->where($map)->delete()){
			$this->_update_cache_tree($tmp['group_id']);
			$this->success('删除成功',U('Catsadmin/index'));
		} else {
			$this->error('删除失败！');
		}
	}
	
	/**
	 * 删除分类分组
	 */
	public function delgroup(){
		$id = array_unique((array)I('ids',0));
		if ( empty($id) ) {
			$this->error('请选择要操作的数据!');
		}
		$map = array($this->mm->getPk() => array('in', $id));
		$tmp = $this->mm->where($map)->find(); //去除一个样本，获取到appid;
		if($this->mm->where($map)->delete()){
			$this->_update_cache_groups();
			
			$mapp['group_id'] = $tmp['group_id'];
			$this->m->where($mapp)->delete();
			F('Cats/group_'.$tmp['group_id'],''); //清空该分组下面的分类缓存
			
			$this->success('删除成功',U('Catsadmin/index'));
		} else {
			$this->error('删除失败！');
		}
	}
	
	/**
	 * 固化app分类树存储
	 * @param unknown $appid
	 * @param unknown $groupid
	 */
	public function _update_cache_tree($groupid){
		$tree = new \Think\Tree();
		
		$map['group_id'] = $groupid;
		$data = $this->m->where($map)->order('listorder desc')->select();
		
		$tree->init($data);
		$treedata = $tree->get_tree_array(0, $str);
		
		F('Cats/group_'.$groupid,$treedata);
		return $treedata;
	}
	
	/**
	 * 固化app分类分组数据
	 * @param unknown $appid
	 */
	public function _update_cache_groups(){
		$data = $this->mm->where($map)->select();
		foreach($data as $d){
			$result[$d['group_key']] = $d;
		}
		F('Cats/groups/',$result);
		return $result;
	}
	
	/**
	 * ajax 获取tree插件节点数据
	 */
	public function ajax_nodes(){
		$tree = new \Think\Tree();
		
		$map['group_id'] = $_REQUEST['groupid'];
		$data = $this->m->where($map)->order('listorder desc')->select();
		
		$tree->init($data);
		$treedata = F('Cats/group_'.$_REQUEST['groupid']);
		if(!$treedata){
			$treedata = $this->_update_cache_tree( $_REQUEST['groupid']);
		}
		
		$results = array_filter(explode('|', $this->_get_node_tpl($treedata, $result)));
		$last = array();
		foreach($results as $r){
			$last[] = json_decode($r,true);
		}
		//echo json_encode($last);
		$this->ajaxReturn($last);
	}
	
	/**
	 * 配合ajax_nodes
	 * @param unknown $datas
	 * @param unknown $result
	 * @return string
	 */
	public function _get_node_tpl($datas,$result){
		$results = '';
		foreach ($datas as $data){
			$res= array(
					'id' => $data['id'],
					'pId' => $data['parentid'],
					'name' => $data['cat_name'],
					'open' => true
			);
			$results .= '|'.json_encode($res);
			if($data['child']){
				$results .= '|'.$this->_get_node_tpl($data['child'], $result);
			}
		}
		return $results;
	}
}
?>